﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using CourseSystem.Stat.ActionFilter;

namespace CourseSystem.Web.Controllers
{
    [UserAuth(UserType = UserTypeEnum.Teacher)]
    public class SettingController : BaseController
    {
        //
        // GET: /Setting/
        [UrlStatFilter]
        public ActionResult Index()
        {
            var info = CourseSettingBLL.GetFromDb();
            if (info == null)
                return NotFound();
            return View(info);
        }

        public ActionResult Edit()
        {
            var info = CourseSettingBLL.GetFromDb();
            if (info == null)
                return NotFound();
            return View(info);
        }

        [HttpPost]
        public ActionResult Index(CourseSettingInfo info)
        {
            var oldInfo = CourseSettingBLL.GetFromDb();
            if (oldInfo == null)
                return NotFound();
            ViewData.Model = oldInfo;
            if (!ModelState.IsValid)
                return View();
            if (!info.QuestionState.Contains(":") || info.QuestionState.Contains("；") || info.QuestionState.Contains("："))
                ModelState.AddModelError("QuestionState", "格式错误");
            if (!info.PostState.Contains(":") || info.PostState.Contains("；") || info.PostState.Contains("："))
                ModelState.AddModelError("PostState", "格式错误");
            if (!ModelState.IsValid)
                return View();

            var fileArray = new[] { "LoginImgLogoFile", "LoginImgLeftFile", "LoginedImgTopFile", "AdminImgTopFile" };
            for (var i = 0; i < fileArray.Length; i++)
            {
                var msg = "";
                var width = 0;
                var height = 0;
                var fileTitle = "";
                switch (i)
                {
                    case 0:
                        width = 800;
                        height = 158;
                        fileTitle = "登录页面顶部图片";
                        break;
                    case 1:
                        width = 401;
                        height = 439;
                        fileTitle = "登录页面左边图片";
                        break;
                    case 2:
                        width = 1000;
                        height = 168;
                        fileTitle = "登录系统后顶部图片";
                        break;
                    case 3:
                        width = 1000;
                        height = 82;
                        fileTitle = "管理页面顶部图片";
                        break;
                }
                fileArray[i] = ImgHelper.UploadImg(Request.Files[fileArray[i]], ResourcesHelper.UploadImgSize,
                                                           width, height, out msg);
                if (!string.IsNullOrEmpty(msg))
                {
                    SetAlertMsg(fileTitle+msg);
                    return View();
                }
            }
            var fileDels = new[] { "", "", "", "", };
            if (!string.IsNullOrEmpty(fileArray[0]))
            {
                fileDels[0] = oldInfo.LoginImgLogo;
                oldInfo.LoginImgLogo = fileArray[0];
            }
            if (!string.IsNullOrEmpty(fileArray[1]))
            {
                fileDels[1] = oldInfo.LoginImgLeft;
                oldInfo.LoginImgLeft = fileArray[1];
            }
            if (!string.IsNullOrEmpty(fileArray[2]))
            {
                fileDels[2] = oldInfo.LoginedImgTop;
                oldInfo.LoginedImgTop = fileArray[2];
            }
            if (!string.IsNullOrEmpty(fileArray[3]))
            {
                fileDels[3] = oldInfo.AdminImgTop;
                oldInfo.AdminImgTop = fileArray[3];
            }

            oldInfo.CourseCode = info.CourseCode ?? "";
            oldInfo.CourseName = info.CourseName ?? "";
            oldInfo.Teacher = info.Teacher ?? "";
            oldInfo.PostState = info.PostState;
            oldInfo.PostPersan = info.PostPersan;
            oldInfo.ExamPersan = info.ExamPersan;
            oldInfo.HomeworkPersan = info.HomeworkPersan;
            oldInfo.QuestionPersan = info.QuestionPersan;
            oldInfo.QuestionState = info.QuestionState;

            if (CourseSettingBLL.Edit(oldInfo))
            {
                foreach (var item in fileDels)
                {
                    if (string.IsNullOrEmpty(item))
                        continue;
                    System.IO.File.Delete(Server.MapPath("~/") + item);
                }
                SetAlertMsg("编辑成功.");
                ScoreHelper.Reload();
                return View(oldInfo);
            }
            SetAlertMsg("未知错误.");
            return View();

        }

    }
}
